package com.example.demo.responseBodyAdvice.demo.cotroller;

import com.example.demo.responseBodyAdvice.demo.EncryptFilter;
import com.example.demo.responseBodyAdvice.demo.db.entity.UserInfo;
import com.example.demo.responseBodyAdvice.demo.db.repository.UserInfoRepository;
import com.example.demo.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户信息控制器
 *
 * @author huan.fu
 * @date 2018/9/28 - 15:55
 */
@Api(tags = {"ResponseBodyAdvice使用（这里是对整个传参、出参进行统一的解密、加密）"})
@RestController
@RequestMapping("/user-info")
@Slf4j
public class UserInfoController {

    @Autowired
    private UserInfoRepository userInfoRepository;

    @ApiOperation(value = "入参和出参都是密文")
    @PostMapping("/both")
    @EncryptFilter
    public Object both(@RequestBody UserInfo userinfo) {
        return ResultUtil.success("data", userinfo);
    }

    @ApiOperation(value = "入参密文，出参明文")
    @PostMapping("/decryptRequest")
    @EncryptFilter(encryptResponse = false)
    public Object decryptRequest(@RequestBody UserInfo userinfo) {
        return ResultUtil.success("data", userinfo);
    }

    @ApiOperation(value = "入参明文，出参密文")
    @PostMapping("/encryptResponse")
    @EncryptFilter(decryptRequest = false)
    public Object encryptResponse(@RequestBody UserInfo userinfo) {
        userInfoRepository.save(userinfo);
        return ResultUtil.success("data", userinfo);
    }

    @ApiOperation(value = "入参和出参都是明文")
    @PostMapping("/non")
    @EncryptFilter(decryptRequest = false, encryptResponse = false)
    public Object non(@RequestBody UserInfo userinfo) {
        List<UserInfo> data = userInfoRepository.findAll();
        return ResultUtil.success("data", data);
    }
}